package me.midday.java.base.knowledge.mutlthread;

public class Account {
    private String accountNo;
    private double balance;

    public String getAccountNo() {
        return accountNo;
    }

    public void setAccountNo(String accountNo) {
        this.accountNo = accountNo;
    }

    public double getBalance() {
        return balance;
    }

    public void setBalance(double balance) {
        this.balance = balance;
    }

    public Account(){}

    public Account(String accountNo, double balance){
        this.accountNo = accountNo;
        this.balance = balance;
    }

    @Override
    public int hashCode(){
        return accountNo.hashCode();
    }

    @Override
    public boolean equals(Object obj) {
        if(this == obj){
            return true;
        }
        if (obj != null && obj.getClass() == Account.class){
             Account target = (Account)obj;
             return target.getAccountNo().equals(this.accountNo);
        }
        return false;
    }

    public synchronized void draw(double drawAmount){

        if (balance > drawAmount){
            System.out.println(Thread.currentThread().getName() + "取钱成功: " + drawAmount);
            balance -= drawAmount;
            System.out.println("\t余额为： " + balance);
        }else {
            System.out.println(Thread.currentThread().getName() + "取钱失败!余额不足！");
        }

    }
}
